﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace GalaxyPresident
{
    public abstract class PlayScreenLayer : GameBase.VisibleGroup
    {
        protected PlayingScreen _playingScreen;
        protected VisibleGroup layer;
        
        protected Vector2 pressedPoint;
        private float targetScale = 1;
        protected float maxScale=1f;
        protected float minScale=0.3f;
        protected float scaleRate = 0.8f;
        protected float scaleSpeed = 0.96f;
        protected Vector2 topLeft;
        protected Vector2 bottomRight;
        protected bool mousePressed = false;
        protected Vector2 margin = new Vector2(Constant.screenWidth, Constant.screenHeight) / 2;

        protected MotionEffects layerMotion = new MotionEffects(3);

        protected List<Team> _teams;


        public VisibleGroup Layer
        {
            get { return layer; }
            set { layer = value; }
        }
        protected float TargetScale
        {
            get { return targetScale; }
            set { 
                targetScale = value;
                if (targetScale < minScale)
                {
                    targetScale = minScale;
                }
                else if (TargetScale > maxScale)
                {
                    targetScale = maxScale;
                }
            }
        }

        protected Game1 Game
        {
            get { return _playingScreen.Game; }
            set { _playingScreen.Game = value; }
        }

        protected MouseState LastMouseState
        {
            get { return _playingScreen.LastMouseState; }
        }


        protected MouseState MouseState
        {
            get { return _playingScreen.MouseState; }
        }
        public PlayScreenLayer(PlayingScreen playingScreen)
        {
            _playingScreen = playingScreen;
            _teams = playingScreen.Teams;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            layerMotion.Update(gameTime);
            if (layerMotion.Velocity != Vector2.Zero)
            {
                TranslateScreen(layerMotion.Velocity.X, layerMotion.Velocity.Y);
            }
            /*#region Zoom and Scroll processing
            mouseState = Mouse.GetState();
            scrollScreen();
            zoomScreen();
            //
            
            MouseHandle();
            
            handleKeyBoard();
            lastMouseState = mouseState;
            #endregion*/
        }
        public virtual void HandleKeyBoard()
        {
        }
        protected bool IsMouseMoved()
        {
            return !(pressedPoint.X == MouseState.X && pressedPoint.Y == MouseState.Y);
        }
        public virtual void MouseHandle()
        {
            zoomScreen();
            scrollScreen();
        }
        public virtual void MousePressed(bool onForm)
        {
            mousePressed = !onForm;
            pressedPoint = new Vector2(MouseState.X, MouseState.Y);
        }
        public virtual void MouseReleased(bool onForm)
        {
            mousePressed = false;
        }
        protected virtual void zoomScreen()
        {
            float scrollValue = MouseState.ScrollWheelValue - LastMouseState.ScrollWheelValue;
            if (scrollValue > 0)
            {
                TargetScale /= scaleRate;
            }
            else if (scrollValue < 0)
            {
                TargetScale *= scaleRate;
            }

            float r = Scale / TargetScale;
            
            if (r <= scaleSpeed)
            {
                Scale /= scaleSpeed;
            }
            else if (r >= 1/scaleSpeed)
            {
                Scale *= scaleSpeed;
            }
            else
            {
                Scale = TargetScale;
            }
        }
        protected Vector2 GetDeltaMousePosition()
        {
            return new Vector2(MouseState.X - LastMouseState.X, MouseState.Y - LastMouseState.Y);
        }
        protected virtual void TranslateScreen(float x, float y)
        {
            Layer.Translate(x, y);
        }
        protected virtual Vector2 GetScreenPosition()
        {
            return Layer.Position;
        }
        protected virtual void scrollScreen()
        {
            Vector2 dpos = new Vector2(MouseState.X -LastMouseState.X,
                        MouseState.Y - LastMouseState.Y)/Scale;
            if (LastMouseState.LeftButton != MouseState.LeftButton )
            {
                if (MouseState.LeftButton == ButtonState.Released)
                {
                    layerMotion.Position = Layer.Position;
                    if (dpos != Vector2.Zero)
                    {
                        float d = dpos.Length() + 20;
                        dpos.Normalize();
                        if (d > 80)
                        {
                            d = 80;
                        }

                        layerMotion.Velocity = dpos * d;
                    }
                }
                else
                {
                    pressedPoint = new Vector2(MouseState.X, MouseState.Y);
                }
            }
            if (MouseState.LeftButton == ButtonState.Pressed && mousePressed)
            {
                TranslateScreen(dpos.X, dpos.Y);
            }
            else
            {
                float autoScrollSpeed = 10;
                if (GetScreenPosition().X < topLeft.X)
                {
                    float d=topLeft.X-GetScreenPosition().X;
                    if (d > autoScrollSpeed)
                    {
                        TranslateScreen(autoScrollSpeed, 0);
                    }
                    else
                    {
                        TranslateScreen(d, 0);
                    }
                }
                else if (GetScreenPosition().X > bottomRight.X)
                {
                    float d = GetScreenPosition().X - bottomRight.X;
                    if (d > autoScrollSpeed)
                    {
                        TranslateScreen(-autoScrollSpeed, 0);
                    }
                    else
                    {
                        TranslateScreen(-d, 0);
                    }
                }
                if (GetScreenPosition().Y < topLeft.Y)
                {
                    float d = topLeft.Y - GetScreenPosition().Y;
                    if (d > autoScrollSpeed)
                    {
                        TranslateScreen(0,autoScrollSpeed);
                    }
                    else
                    {
                        TranslateScreen(0, d);
                    }
                }
                else if (GetScreenPosition().Y > bottomRight.Y)
                {
                    float d = GetScreenPosition().Y - bottomRight.Y;
                    if (d > autoScrollSpeed)
                    {
                        TranslateScreen(0,-autoScrollSpeed);
                    }
                    else
                    {
                        TranslateScreen(0,-d);
                    }
                }
            }

            if (GetScreenPosition().X < topLeft.X - margin.X * Scale)
            {
                TranslateScreen(topLeft.X - margin.X * Scale - GetScreenPosition().X, 0);
            }
            else if(GetScreenPosition().X > bottomRight.X + margin.X * Scale)
            {
                TranslateScreen(bottomRight.X + margin.X * Scale - GetScreenPosition().X, 0);
            }
            if (GetScreenPosition().Y < topLeft.Y - margin.Y * Scale)
            {
                TranslateScreen(0,topLeft.Y - margin.Y * Scale - GetScreenPosition().Y);
            }
            else if (GetScreenPosition().Y > bottomRight.Y + margin.Y * Scale)
            {
                TranslateScreen(0,bottomRight.Y + margin.Y * Scale - GetScreenPosition().Y);
            }
            
        }
    }
}
